package com.hlhj.funlike_business.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.FoodPhotoAdp;
import com.hlhj.funlike_business.bean.BitmapEvent;
import com.hlhj.funlike_business.bean.BitmapEvent1;
import com.hlhj.funlike_business.bean.ShopInfoBean;
import com.hlhj.funlike_business.bean.ShopPhotoBean;
import com.hlhj.funlike_business.bean.UpImgBean;
import com.hlhj.funlike_business.bean.UpImgBean1;
import com.hlhj.funlike_business.presenter.PhotoPresenter;
import com.hlhj.funlike_business.utils.FileUtils;
import com.hlhj.funlike_business.utils.UpImgDialog;
import com.hlhj.funlike_business.views.PhotoView;
import com.huantansheng.easyphotos.EasyPhotos;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PhotoAty extends BaseActivity<PhotoView,PhotoPresenter> implements PhotoView {
    private ImageView img_back;
    private TextView tv_title;
    private RecyclerView recycle_list;
    private FoodPhotoAdp foodPhotoAdp;
    private List<String> imgStrings;
    private List<ShopPhotoBean.DataBean> imglist;
    public final static int TAKE_PHOTO = 1;
    private String title;
    private UpImgDialog imgDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int getContentId() {
        return R.layout.aty_photo;
    }

    @Override
    protected void initData() {
        tv_title.setText(title);

        imgDialog=new UpImgDialog(this);
        imgDialog.setCancelable(false);
        imgStrings = new ArrayList<>();
        imglist = new ArrayList<>();

        foodPhotoAdp = new FoodPhotoAdp(this,imgStrings);
        GridLayoutManager manager = new GridLayoutManager(this,2);
        recycle_list.setLayoutManager(manager);
        recycle_list.setAdapter(foodPhotoAdp);
    }

    @NotNull
    @Override
    public PhotoView bindView() {
        return this;
    }

    @NotNull
    @Override
    public PhotoPresenter createPresenter() {
        return new PhotoPresenter();
    }

    @Override
    public void initView() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        recycle_list = findViewById(R.id.recycle_list);

        title=getIntent().getStringExtra("title");
    }

    @Override
    public void logicStart() {
        if (title.equals("相册")){
            getPresenter().LoadPhoto(this);
        }else {
            getPresenter().LoadQualifaction(this);
        }

        foodPhotoAdp.setListener(new FoodPhotoAdp.getDel() {
            @Override
            public void showDel(int position) {
                if (title.equals("相册")){
                    getPresenter().LoadDeLImg(PhotoAty.this,imglist.get(position).getId(),3);
                }else {
                    getPresenter().LoadDeLImg(PhotoAty.this,imglist.get(position).getId(),2);
                }
            }
        });
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void getBitmap(BitmapEvent1 bitmapEvent) throws IOException {
        File file = FileUtils.INSTANCE.createImageFile(bitmapEvent.bitmap, this);
        imgDialog.show();
        if (title.equals("相册")){
            getPresenter().LoadUpImgResult(this,file);
        }else {
            getPresenter().UpQualificationImg(this,file);
        }
//        foodPhotoAdp.notifyDataSetChanged();
//        Glide.with(this).load(file).into(img_cover);
    }
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK)return;
        switch (requestCode){
            case TAKE_PHOTO:
                ArrayList<String> listExtra = data.getStringArrayListExtra(EasyPhotos.RESULT_PATHS);
                File file = new File(listExtra.get(0).toString());
                imgDialog.show();
                if (title.equals("相册")){
                    getPresenter().LoadUpImgResult(PhotoAty.this,file);
                }else {
                    getPresenter().UpQualificationImg(PhotoAty.this,file);
                }
                break;
        }
    }


    @Override
    public void showPhoto(List<ShopPhotoBean.DataBean> dataBeans) {
        imgStrings.clear();
        imglist.clear();
        for (ShopPhotoBean.DataBean dataBean : dataBeans){
            imgStrings.add(dataBean.getImage());
        }
        imglist.addAll(dataBeans);
        foodPhotoAdp.notifyDataSetChanged();
    }

    @Override
    public void showUpImgResult(UpImgBean1.DataBean dataBeans) {
        Toast.makeText(PhotoAty.this,"上传成功",Toast.LENGTH_SHORT).show();
        imgStrings.add(dataBeans.getUrl());
        foodPhotoAdp.notifyDataSetChanged();
        imgDialog.dismiss();
        if (title.equals("相册")){
            getPresenter().LoadPhoto(this);
        }
    }

    @Override
    public void showDelResult(String msg) {
        Toast.makeText(PhotoAty.this,msg.toString(),Toast.LENGTH_SHORT).show();
        if (title.equals("相册")){
            getPresenter().LoadPhoto(this);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (imgDialog.isShowing()){
                imgDialog.dismiss();
                Toast.makeText(PhotoAty.this,"取消上传",Toast.LENGTH_SHORT).show();
                return true;
            }
            EventBus.getDefault().postSticky("刷新我的界面");
        }
        return super.onKeyDown(keyCode, event);
    }
}
